package com.et.util;
/**
 * 直接获取Spring依赖注入的对象
 */
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
 
@Service
public class SpringContextUtils implements ApplicationContextAware {
 
    private static ApplicationContext applicationContext;
 
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }
 
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
 
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        if (applicationContext == null){
        	return null;
        }
        return (T)applicationContext.getBean(name); 
    }
}
